/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/
 * @date:        2019/5/20 20:57
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.common.util;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.http.util.Asserts;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * CapsuleStringUtil 自定义字符串类方法
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019-06-30 12:40
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019-06-30
 * @modify reason: {方法名}:{原因}
 * ...
 */
public class CapStringUtil {

    /**
     * 数字转16进制字符串
     *
     * @param code
     * @return
     */
    public static String integerToHexString(Integer code) {
        String hexVal = Integer.toHexString(code).toLowerCase();
        String[] hexArray = new String[]{"0", "x", "0", "0", "0", "0", "0", "0", "0", "0"};
        char[] chars = hexVal.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            hexArray[hexArray.length - chars.length + i] = String.valueOf(chars[i]);
        }
        StringBuilder sb = new StringBuilder();
        Arrays.stream(hexArray).forEach(sb::append);
        return sb.toString();
    }

    /**
     * 对象是否为无效值
     *
     * @param obj 要判断的对象
     * @return 是否为有效值(不为null 和 " " 字符串)
     */
    public static boolean isNullOrEmpty(Object obj) {
        return obj == null || "".equals(obj.toString());
    }

    /**
     * 将字符串的第一位转为小写
     *
     * @param str 需要转换的字符串
     * @return 转换后的字符串
     */
    public static String toLowerCaseFirstOne(String str) {
        if (Character.isLowerCase(str.charAt(0))) {
            return str;
        } else {
            char[] chars = str.toCharArray();
            chars[0] = Character.toLowerCase(chars[0]);
            return new String(chars);
        }
        // CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, modelNameUpperCamel)
    }

    /**
     * 将字符串的第一位转为大写
     *
     * @param str 需要转换的字符串
     * @return 转换后的字符串
     */
    public static String toUpperCaseFirstOne(String str) {
        if (Character.isUpperCase(str.charAt(0))) {
            return str;
        } else {
            char[] chars = str.toCharArray();
            chars[0] = Character.toUpperCase(chars[0]);
            return new String(chars);
        }
    }

    /**
     * 下划线命名转为驼峰命名
     *
     * @param str 下划线命名格式
     * @return 驼峰命名格式
     */
    public static String underScoreCase2CamelCase(String str) {
        if (!str.contains("_")) {
            return str;
        }
        StringBuilder sb = new StringBuilder();
        char[] chars = str.toCharArray();
        boolean flag = false;
        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            if (ch == '_') {
                flag = true;
            } else {
                if (flag) {
                    sb.append(Character.toUpperCase(ch));
                    flag = false;
                } else {
                    sb.append(ch);
                }
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰命名转为下划线命名
     *
     * @param str 驼峰命名格式
     * @return 下划线命名格式
     */
    public static String camelCase2UnderScoreCase(String str) {
        StringBuilder sb = new StringBuilder();
        char[] chars = str.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            if (Character.isUpperCase(ch)) {
                sb.append("_").append(Character.toLowerCase(ch));
            } else {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

    /**
     * 生成随机值
     *
     * @param clazz
     * @param len
     * @param <T>
     * @return
     */
    public static <T> T generatorRondomValue(Class<T> clazz, Integer len) {
        String clazzName = clazz.getName();
        Object obj = null;
        switch (clazzName) {
            case "java.lang.Integer":
                obj = (int) (Math.random() * len);
                break;
            case "java.lang.String":
                obj = getRandomString(len);
                break;
            case "java.lang.Byte":
                obj = new Byte("xx");
                break;
            case "java.lang.Double":
                obj = Math.random() * len;
                break;
            case "java.lang.Short":
                obj = Short.valueOf("x");
                break;
            case "java.lang.Long":
                obj = (long) (Math.random() * len);
                break;
            case "java.lang.Boolean":
                obj = System.currentTimeMillis() / 1000 % 2 == 0;
                break;
            case "java.math.BigDecimal":
                obj = BigDecimal.valueOf(Math.random() * len);
                break;
        }
        return clazz.cast(obj);
    }

    /**
     * 生成随机字符串
     *
     * @return
     * @author Yi
     * @date 2019/1/26 13:59
     * @modify by: {修改人} 2019/1/26 13:59
     * @modify by reason:
     * @since 1.0.0
     */
    public static String getRandomString(int length) {
        //定义一个字符串（A-Z，a-z，0-9）即62位；
        String str = "zxcvbnmlkjhgfdsaqwertyuiopQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
        //由Random生成随机数
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        //长度为几就循环几次
        for (int i = 0; i < length; ++i) {
            //产生0-61的数字
            int number = random.nextInt(62);
            //将产生的数字通过length次承载到sb中
            sb.append(str.charAt(number));
        }
        //将承载的字符转换成字符串
        return sb.toString();
    }

    /**
     * 生成ACSII随机字符串
     *
     * @return
     * @author Yi
     * @date 2019/1/26 14:00
     * @modify by: {修改人} 2019/1/26 14:00
     * @modify by reason:
     * @since 1.0.0
     */
    public static String getRandomStringWithASCIIValue(int length) {
        //产生随机数
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        //循环length次
        for (int i = 0; i < length; i++) {
            //产生0-2个随机数，既与a-z，A-Z，0-9三种可能
            int number = random.nextInt(3);
            long result = 0;
            switch (number) {
                //如果number产生的是数字0；
                case 0:
                    //产生A-Z的ASCII码
                    result = Math.round(Math.random() * 25 + 65);
                    //将ASCII码转换成字符
                    sb.append(String.valueOf((char) result));
                    break;
                case 1:
                    //产生a-z的ASCII码
                    result = Math.round(Math.random() * 25 + 97);
                    sb.append(String.valueOf((char) result));
                    break;
                case 2:
                    //产生0-9的数字
                    sb.append(String.valueOf
                            (new Random().nextInt(10)));
                    break;
            }
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println("20190703235424".substring(8, 10));
    }

    /**
     * split 获取字符数组后返回最后一个key
     * @param str
     * @param splitRegex
     * @return
     */
    public static String getLastStrWithSplitChar(String str, String splitRegex) {
        Asserts.notEmpty(str,"getLastStrWithSplitChar: str");
        Asserts.notEmpty(splitRegex,"getLastStrWithSplitChar: splitRegex");
        List<String> strings = Arrays.asList(str.split(splitRegex));
        Asserts.check(CollectionUtils.isNotEmpty(strings),"getLastStrWithSplitChar: str spilt get empty list!");
        return strings.get(strings.size() - 1);
    }
}
